package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.EBuy;
import com.chhuang.ewcrm.entity.EBuyGoods;
import com.chhuang.ewcrm.service.EBuyGoodsService;
import com.chhuang.ewcrm.service.EBuyService;
import com.chhuang.ewcrm.string.MoneyUtil;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (EBuy)表控制层
 *
 * @author chhuang
 * @since 2020-11-15 07:37:41
 */
@RestController
@RequestMapping("/service/eBuy")
public class EBuyController {
    Logger logger = LoggerFactory.getLogger(EBuyController.class);
    /**
     * 服务对象
     */
    @Resource
    private EBuyService eBuyService;
    @Resource
    private EBuyGoodsService eBuyGoodsService;


    /**
     * 确认记录完成
     * @param buyId
     * @param customerNetWeight
     * @param buyPrice
     * @param freightCosts
     * @param deductionMoney
     * @param remark2
     * @return
     */
    @PostMapping("/confirmCompletion")
    public ResultBean<?> confirmCompletion(String buyId, Double customerNetWeight, Double buyPrice,
                                           Double freightCosts, Double deductionMoney, String remark2){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        if(StringUtils.isNotEmpty(buyId)) {
            EBuy eBuy = this.eBuyService.queryById(buyId);
            if (eBuy != null) {
                if (eBuy.getIsDelivery()) {//是自提
                    eBuy.setCustomerNetWeight(customerNetWeight);
                    eBuy.setBuyPrice(buyPrice);
                    eBuy.setFreightCosts(freightCosts);
                    eBuy.setDeductionMoney(deductionMoney);

                    eBuy.setPaymentAmount(eBuy.getCustomerNetWeight()*eBuy.getBuyPrice()-(eBuy.getDeductionMoney()==null?0:eBuy.getDeductionMoney()));
                    eBuy.setPaymentAmountCh(MoneyUtil.moneyToChinese(BigDecimal.valueOf(eBuy.getPaymentAmount())));
                    eBuy.setTotalAmount(eBuy.getPaymentAmount()+eBuy.getFreightCosts());
                    eBuy.setBuyAveragePrice(eBuy.getTotalAmount()/eBuy.getNetWeight());
                }
                eBuy.setRemark2(remark2);
                eBuy.setStatus(2);
                Date now = new Date();
                eBuy.setModifyTime(now);
                this.eBuyService.update(eBuy);
                resultBean.setSuccess(true);

                EBuyGoods goods = eBuyGoodsService.queryById(eBuy.getBuyGoodsId());
                if(goods!=null){
                    goods.setBuyTotalWeight(goods.getBuyTotalWeight()+eBuy.getNetWeight());
                    goods.setBuyTotalAmount(goods.getBuyTotalAmount()+eBuy.getTotalAmount());
                    goods.setBuyAveragePrice(goods.getBuyTotalAmount()/goods.getBuyTotalWeight());
                    goods.setModifyTime(now);
                    eBuyGoodsService.update(goods);
                }else {
                    logger.error("添加的新采购记录，对应的物品已经不存在");
                }
            }else {
                resultBean.setMessage("称重记录不存在");
            }
        }else {
            resultBean.setMessage("参数不为空");
        }

        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param buyId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<EBuy> getOne(String buyId) {
        ResultBean<EBuy> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(buyId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eBuyService.queryById(buyId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<EBuy>> query(@RequestBody QueryBean<EBuy> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<EBuy>> resultBean = new ResultBean<>();

        PageInfo<EBuy> pageInfo = null;
        List<EBuy> buys = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eBuyService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eBuyService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }
}